package com.twoGroup.boot.service.imp;


import com.twoGroup.boot.controller.AddressController;
import com.twoGroup.boot.mapper.MerchantMapper;
import com.twoGroup.boot.mapper.UserMapper;
import com.twoGroup.boot.pojo.Goods;
import com.twoGroup.boot.pojo.GoodsAndShopAndGoodsTypeAndGoodsBrand;
import com.twoGroup.boot.pojo.GoodsImg;
import com.twoGroup.boot.pojo.Shop;
import com.twoGroup.boot.service.MerchantService;
import com.twoGroup.boot.utils.QiniuCloudUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Xuyujie
 * @Date 2021/5/15 8:47
 * @Version 1.0
 **/
@Service
public class MerchantServiceImp implements MerchantService {
    public static Logger logger = LoggerFactory.getLogger(MerchantServiceImp.class);

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addShop(Shop shop) {
        return merchantMapper.addShop(shop);
    }

    //关闭店铺
    @Override
    public int shutDownShopById(int shopId) {
        return merchantMapper.shutDownShop(shopId);
    }

    //上架商品
    @Override
    @Transactional
    public int addGoods(Goods goods) {
        goods.setGoodsSales(0);
        return merchantMapper.addGoods(goods);
    }

    //下架商品
    @Transactional
    @Override
    public int deleteGoods(int goodsId) {
        QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();
        logger.info(String.valueOf(goodsId));
        Goods goods = merchantMapper.queryForGoods(goodsId);
        logger.info(String.valueOf(goods));
        if (goods != null) {
            try {
                qiniuCloudUtil.delete(goods.getGoodsImg());
                List<GoodsImg> list = merchantMapper.queryForGoodsImg(goodsId);
                if (list.size() != 0) {
                    for (GoodsImg goodsImg : list) {
                        qiniuCloudUtil.delete(goodsImg.getGoodsImg());
                    }
                }
                merchantMapper.deleteImg(goodsId);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return merchantMapper.deleteGoods(goodsId);
        } else {
            return 0;
        }
    }

    @Override
    public int updateGoods(Goods goods) {
        return merchantMapper.updateGoods(goods);
    }

    @Override
    public boolean judgeShopName(String shopName) {
        if (merchantMapper.queryShopName(shopName) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean judgeShop(int shopId) {
        if (merchantMapper.queryForShop(shopId) == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Goods queryForGoods(int goodsId) {
        return merchantMapper.queryForGoods(goodsId);
    }

    @Override
    public int addImg(GoodsImg goodsImg) {
        return merchantMapper.addGoodsImg(goodsImg);
    }

    @Override
    public List<GoodsImg> queryForGoodsImg(int goodsId) {
        return merchantMapper.queryForGoodsImg(goodsId);
    }

    @Override
    public GoodsImg queryForGoodsImgByImgId(int imgId) {
        return merchantMapper.queryForGoodsImgByImgId(imgId);
    }

    @Override
    public int deleteImgByImgId(Integer imgId) {
        return merchantMapper.deleleImgByImgId(imgId);
    }

    @Override
    public int queryForUserShop(int userId) {
        return merchantMapper.queryForUserShop(userId);
    }

    @Override
    public List<GoodsAndShopAndGoodsTypeAndGoodsBrand> queryForGoodsAll(int shopId) {
        return merchantMapper.queryForGoodsAll(shopId);
    }

    @Override
    public List<Shop> queryForUserShopById(int userId) {
        return merchantMapper.queryForUserShopById(userId);
    }

    @Override
    public List<Integer> queryForMerchant() {
        return merchantMapper.querForMerchant();
    }
}
